package com.xxxx.yoyo.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.yoyo.bean.Type;
import com.xxxx.yoyo.bean.User;
import com.xxxx.yoyo.service.ITypeService;
import com.xxxx.yoyo.service.IUserService;
import com.xxxx.yoyo.service.impl.TypeServiceImpl;
import com.xxxx.yoyo.service.impl.UserServiceImpl;
import com.xxxx.yoyo.util.SafeUtil;

/**
 * 重置密码
 * Servlet implementation class ForgetPasswordServlet
 */
@WebServlet("/index/forget")
public class ForgetPasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ITypeService typeService = new TypeServiceImpl();
	private IUserService userService = new UserServiceImpl();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置导航条上的 [登录] 处于选中状态
		request.setAttribute("flag", 6);
		
		//获取 [宠物分类] 列表
		List<Type> typeList = typeService.getTypeList();
		//把typeList存入request中, 供/index/forget.jsp
		request.setAttribute("typeList", typeList);
		
		//请求转发,跳转到/index/forget.jsp
		request.getRequestDispatcher("/index/forget.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码格式为UTF-8,解决中文参数乱码
		request.setCharacterEncoding("UTF-8");
		
		//获取前端传递过来的参数
		String username = request.getParameter("username");
		String phone = request.getParameter("phone");
		
		//看看用户名是否存在----根据用户名查询用户
		User user = userService.getUserByUsername(username);
		if(user == null) {//没有查到用户----这个用户不存在
			//提示错误信息
			request.setAttribute("msg", "用户不存在,请检查是否输入错误!");			
		}else {//有这个用户
			//验证手机号是否正确. ---查到的手机号和用户提交的手机号是否一致
			//获取查询到的手机号
			String phone2 = user.getPhone();
			if(phone.equals(phone2)) {//如果手机号一致---可以重置密码
				//把用户的密码设置为默认密码(即123456)
				//对默认密码加密
				String pwd = SafeUtil.encode("123456");
				//更新user对象的密码
				user.setPassword(pwd);
				//把用户信息更新到数据库中.
				boolean isSuccess = userService.updateUser(user);
				if(isSuccess == true) {//更新成功
					//提示成功信息
					request.setAttribute("msg", "密码重置成功,新密码是123456");
				}else {
					//提示失败信息
					request.setAttribute("msg", "密码重置失败,请稍后再试!");
				}
				
			}else {//手机号不一致----不能充值密码
				//提示错误信息
				request.setAttribute("msg", "手机号与绑定的手机不一致,请查证后再重置密码!");
			}
		}
		//设置导航条上的 [登录] 处于选中状态
		request.setAttribute("flag", 6);
		
		//获取 [宠物分类] 列表
		List<Type> typeList = typeService.getTypeList();
		//把typeList存入request中, 供/index/forget.jsp
		request.setAttribute("typeList", typeList);
		
		//请求转发,跳转到/index/forget.jsp页面
		request.getRequestDispatcher("/index/forget.jsp").forward(request, response);

	}

}
